package risk.client.logic;

import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;

import risk.client.Risk;
import java.awt.Color;
import java.awt.event.ActionListener;


/**
 * @author: Mike
 */
public class Human extends Player implements MouseListener {
    private int x, y;
    
    /**
     * @author: Mike
     * @param risk
     */
    public Human(int id, String name, Color color, Risk risk) {
        super(id, name, color, risk);
    }

    /**
     * Get selected node
     * 
     * @author: Mike
     * @return node
     */
    public Node getSelectedNode(){
        Node node = null;
        Thread thread;

        // Activate button
        ActionListener listener = new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e) {
                    endTurn(true);
                }
            };
        risk.frame.btnEndTurn.addActionListener(listener);
            
        // Listen on nodeclicks
        MouseListener clickListener = new MouseAdapter(){
            public void mousePressed(MouseEvent e) {
                risk.board.setSelectedNode(risk.board.getGrid().getNode(e.getX(), e.getY()));
            }
        };
        risk.board.addMouseListener(clickListener);
        
        // Wait for player to select a node or end turn    
        while(!this.endedTurn() && risk.board.getSelectedNode() == null && risk.running()) {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
            }
        }

        risk.frame.btnEndTurn.removeActionListener(listener);
        risk.board.removeMouseListener(clickListener);
        
        // Get selected node, and reset setSelectedNode
        node = risk.board.getSelectedNode();
        
        return node;
    }
    
    /**
     * Mouse Clicked event
     * 
     * @author: Mike
     * @param e
     */
    public void mouseClicked(MouseEvent e) {
        x = e.getX();
        y = e.getY();
    }

    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
}
